package com.example.homework7;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;
import androidx.fragment.app.Fragment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class waibu extends Fragment {
    public waibu(){super(R.layout.waibu);}

    private Button button3;
    private  Button button4;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button button3=(Button) getView().findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    File externalPath=getActivity().getExternalFilesDir(null);
                    File file=new File(externalPath,"waibucunchu.txt");
                try{
                    FileOutputStream os=new FileOutputStream(file);
                    BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(os));
                    writer.write("Tom");
                    writer.close();
                    Toast.makeText(getActivity(),"写入成功",Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        Button button4=(Button) getView().findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File externalPath=getActivity().getExternalFilesDir(null);
                File file =new File(externalPath,"waibucunchu.txt");
                try{
                    FileInputStream is=new FileInputStream(file);
                    BufferedReader reader=new BufferedReader(new InputStreamReader(is));
                    String line=reader.readLine();
                    Toast.makeText(getActivity(),line,Toast.LENGTH_SHORT).show();
                    is.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
